package com.itheima.controller;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.input.KeyCode;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.util.Duration;

import java.util.concurrent.atomic.AtomicBoolean;

public class MediaViewFx {
    @FXML
    private MediaView mediaView;

    //相当于一个fxml布局文件对应一个controller MediaViewFx就是一个controller类
    //fxml文件就是布局  controller就写逻辑


    public void setMediaPlayer( String path) {
        //如果为MP3格式，则不播放视频 播放音频
        if (path.endsWith(".mp3")){
            System.out.println(path);
            MediaPlayer mediaPlayer = new MediaPlayer(new Media(path));
            mediaPlayer.setOnReady(mediaPlayer::play);
            mediaView.setMediaPlayer(mediaPlayer);
        }else {
            System.out.println("setMediaPlayer");
            Media media = new Media(path);

            MediaPlayer mediaPlayer = new MediaPlayer(media);
            mediaView.setMediaPlayer(mediaPlayer);
            mediaView.setFocusTraversable(true);  // 确保 MediaView 可以获得焦点
            mediaPlayer.setOnReady(() -> {

                Platform.runLater(() -> {
                    mediaView.getScene().getWindow().setWidth(mediaPlayer.getMedia().getWidth());
                    mediaView.getScene().getWindow().setHeight(mediaPlayer.getMedia().getHeight()+45);
                });
                mediaPlayer.play(); // 等 Media 加载完成后再播放
            });
            AtomicBoolean isPlaying = new AtomicBoolean(true);
            AtomicBoolean isPlaying_click = new AtomicBoolean(true);

            mediaView.setOnKeyPressed(event -> {
                //键盘右键加五秒，键盘左键减五秒
                if(event.getCode() == KeyCode.RIGHT){
                    mediaPlayer.seek(mediaPlayer.getCurrentTime().add(Duration.seconds(5)));
                }
                if(event.getCode() == KeyCode.LEFT){
                    mediaPlayer.seek(mediaPlayer.getCurrentTime().subtract(Duration.seconds(5)));
                }
                //键盘空格键暂停和播放
                if(event.getCode() == KeyCode.SPACE){
                    if (isPlaying.get()){
                        mediaPlayer.pause();
                        isPlaying.set(false);
                    }else {
                        mediaPlayer.play();
                        isPlaying.set(true);
                    }
                }
                if (event.getCode()==KeyCode.UP){
                    mediaPlayer.setVolume((mediaPlayer.getVolume()+0.1)>1?1:mediaPlayer.getVolume()+0.1);
                }
                if (event.getCode()==KeyCode.DOWN){
                    mediaPlayer.setVolume((mediaPlayer.getVolume()-0.1)<0?0:mediaPlayer.getVolume()-0.1);
                }
            });
            mediaView.setOnMouseClicked(event -> {
                if (isPlaying_click.get()){
                    mediaPlayer.pause();
                    isPlaying_click.set(false);
                }else {
                    mediaPlayer.play();
                    isPlaying_click.set(true);
                }
            });
            //如果播放完成，重复播放
            mediaPlayer.setOnEndOfMedia(() -> {
                mediaPlayer.seek(Duration.ZERO);
            });

        }

    }

    public MediaView getMediaView() {
        return mediaView;
    }
}
